﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BasicForms;
using Lists;
using GeneralLibs;

namespace ReportGenerators.Khac
{
    public partial class FrmThongKeHangXoaBanLe : BasicForms.FrmSupportPrintableComponents
    {
        public FrmThongKeHangXoaBanLe()
        {
            InitializeComponent();
        }

        private void FrmThongKeHangXoaBanLe_Load(object sender, EventArgs e)
        {
            try
            {
                cmbKyBaoCao.SelectedIndex = 19;
                FormatNumberInGridView(gridView);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                FrmThamSoBaoCao.ThietLapKhoangThoiGianBaoCao(cmbKyBaoCao.Text, cmbKyBaoCao, dtNgayBatDau, dtNgayKetThuc);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);

            BasicForms.FrmProgressBar.StartShowProgress();
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand(); cmd.CommandTimeout = Config.TimeOut;

                cmd.CommandText = string.Format("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
                cmd.ExecuteNonQuery();

                cmd.CommandText = string.Format("SELECT FullName, ThoiGian, DienGiai \n"+
                                                "FROM NhatKySuDung  \n"+
                                                "WHERE DienGiai like N'%xóa khoản mục%' AND DienGiai like N'%HĐBL%'  \n"+
                                                "	AND @ThoiDiemDau <= ThoiGian AND ThoiGian <= @ThoiDiemCuoi  \n" +
                                                "ORDER BY ThoiGian DESC \n");

                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ThoiDiemDau", dtNgayBatDau.DateTime);
                cmd.Parameters.AddWithValue("@ThoiDiemCuoi", dtNgayKetThuc.DateTime);

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                gridControl.DataSource = dt;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
                con = null;
            }
            BasicForms.FrmProgressBar.FinishShowProgress();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            PrintWithHeader(true, gridControl, true, true, "Thống kê hàng xóa từ bán lẻ", "");
        }
    }
}
